package cn.jiyun.service.impl;

import cn.jiyun.constant.MessageConstant;
import cn.jiyun.dao.MemberDao;
import cn.jiyun.dao.OrderDao;
import cn.jiyun.dao.OrderSettingDao;
import cn.jiyun.entity.Result;
import cn.jiyun.pojo.Member;
import cn.jiyun.pojo.Order;
import cn.jiyun.pojo.OrderSetting;
import cn.jiyun.serivce.OrderService;
import cn.jiyun.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Result submitOrder(Map<String, Object> map) throws Exception {
//        1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
        String orderDate = (String)map.get("orderDate");
        String telephone = (String)map.get("telephone");
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate);
        if(orderSetting==null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
//        2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
        if(orderSetting.getReservations()>=orderSetting.getNumber()){
            return new Result(false, MessageConstant.ORDER_FULL);
        }
//        4、检查当前用户是否为会员，如果是会员则直接完成预约，如果不是会员则自动完成注册并进行预约
        Member member = memberDao.findByPhone(telephone);
        if(member==null){
//            如果不是会员则自动完成注册并进行预约
            member = new Member();
            member.setName((String)map.get("name"));
            member.setSex((String)map.get("sex"));
            member.setIdCard((String)map.get("idCard"));
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            memberDao.add(member);
        }
//        3、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约 则无法完成再次预约
//        SELECT * FROM t_order WHERE member_id = ? and orderDate = ? and setmeal_id = ?
        map.put("memberId",member.getId());
        Order order = orderDao.find(map);
        if(order!=null){
            return new Result(false, MessageConstant.HAS_ORDERED);
        }
//        5、预约成功，更新当日的已预约人数 实现代码如下：
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.updateByOrderDate(orderSetting);
//        6、记录预约信息
        order = new Order();
        order.setMemberId(member.getId());
//        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        order.setOrderDate(orderSetting.getOrderDate());
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        String setmealId = (String)map.get("setmealId");
        order.setSetmealId(Integer.parseInt(setmealId));
        orderDao.add(order);
        return new Result(true, MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map<String, Object> findById(Integer id) {
        return orderDao.findById(id);
    }
}
